#include "writevideo.h"

WriteVideo::WriteVideo()
{

}

WriteVideo::~WriteVideo()
{

}

void WriteVideo::setInfo(string path_open, string path_save,
                         list<SubtitleInfo> *l_subtitle1,list<BoxInfo> *l_box1)
{
    this->path_open = path_open;
    this->path_save = path_save;
    l_subtitle = l_subtitle1;
    l_box = l_box1;
}

void WriteVideo::run()
{
    capture.open(path_open);
    Mat mat;
    capture >> mat;

    width = capture.get(CV_CAP_PROP_FRAME_WIDTH);
    height = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
    double rate = capture.get(CV_CAP_PROP_FPS);
    speed = 1000/rate;

    Size s(width,height);
    writer = VideoWriter(path_save,CV_FOURCC('M','P','4','2'),speed,s);

    Mat frame(s,CV_8UC3);
    double total_frame = capture.get(CV_CAP_PROP_FRAME_COUNT);
    int temp = total_frame/100;
    int i = 1;
    int value = 0;


    while(!mat.empty())
    {
        count = capture.get(CV_CAP_PROP_POS_FRAMES);
        if(i == temp)
        {
            i = 1;
            ++value;
            emit resultReady(value);
        }

        //保存添加的字幕
        for(list<SubtitleInfo>::iterator iter = l_subtitle->begin();
            iter != l_subtitle->end();iter++)
        {
            if(count >= iter->front_start && count <= iter->front_end)
            {
                if(iter->front_Chinese_sign == 0)
                {
                    cv::putText(frame, iter->front_text,
                                Point(iter->front_x,
                                      iter->front_y),
                                FONT_HERSHEY_TRIPLEX, 0.8, Scalar(iter->front_color_b,
                                                                  iter->front_color_g,
                                                                  iter->front_color_r),
                                0.6, CV_AA);
                }
                if(iter->front_Chinese_sign == 1)
                {
                    QString str = QString::fromStdString(iter->front_text);
                    const wchar_t *theRealSub = reinterpret_cast<const wchar_t *>(str.utf16());
                    switch (iter->front_type) {
                    case 0:
                        cvtext = new CvxText("C:\\Windows\\Fonts\\AdobeSongStd-Light.otf"); //宋体
                        break;
                    case 1:
                        cvtext = new CvxText("C:\\Windows\\Fonts\\AdobeFangsongStd-Regular.otf"); //仿宋
                        break;
                    case 2:
                        cvtext = new CvxText("C:\\Windows\\Fonts\\AdobeKaitiStd-Regular.otf"); //楷体
                        break;
                    case 3:
                        cvtext = new CvxText("C:\\Windows\\Fonts\\msyh.ttc"); //微软雅黑
                        break;
                    case 4:
                        cvtext = new CvxText("C:\\Windows\\Fonts\\AdobeHeitiStd-Regular.otf"); //黑体
                        break;
                    default:
                        break;
                    }
                    Scalar size = iter->front_size;
                    cvtext->setFont(NULL,&size,NULL,NULL);
                    cvtext->putText(frame,theRealSub,Point(iter->front_x,
                                                           iter->front_y),
                                   Scalar(iter->front_color_b,iter->front_color_g,iter->front_color_r));

                    delete cvtext;
                    cvtext = NULL;
                }
            }
        }

        //保存添加的框选
        for(list<BoxInfo>::iterator iter1 = l_box->begin();
            iter1 != l_box->end();iter1++)
        {
            if(count >= iter1->box_start && count <= iter1->box_end)
            {
                switch (iter1->box_type) {
                //画矩形
                case 0:
                    rectangle(mat,Rect(iter1->box_x1,iter1->box_y1,
                                   (iter1->box_x2 - iter1->box_x1),
                                   (iter1->box_y2 - iter1->box_y1)),
                              Scalar(iter1->box_color_b,iter1->box_color_g,
                                     iter1->box_color_r),
                              iter1->box_line,
                              LINE_4);
                    break;
                //画椭圆
                case 1:
                    ellipse(mat,Point(iter1->box_x1 + (iter1->box_x2 -
                                                         iter1->box_x1) / 2,
                                  iter1->box_y1 + (iter1->box_y2 -
                                                   iter1->box_y1) / 2),
                            Size((iter1->box_x2 - iter1->box_x1) / 2,
                                 (iter1->box_y2 - iter1->box_y1) / 2),
                            0,
                            0,
                            360,
                            Scalar(iter1->box_color_b,iter1->box_color_g,
                                   iter1->box_color_r),
                            iter1->box_line,
                            LINE_4);
                    break;
                //画圆形
                case 2:
                    circle(mat,Point(iter1->box_x1 + (iter1->box_x2 -
                                                        iter1->box_x1) / 2,
                                iter1->box_y1 + (iter1->box_y2 -
                                                 iter1->box_y1) / 2),
                           sqrt((iter1->box_x2 - iter1->box_x1) *
                                (iter1->box_x2 - iter1->box_x1)
                                + (iter1->box_y2 - iter1->box_y1) *
                                (iter1->box_y2 - iter1->box_y1)) / 2,
                           Scalar(iter1->box_color_b,iter1->box_color_g,
                                  iter1->box_color_r),
                           iter1->box_line,
                           LINE_4);
                    break;
                //画直线
                case 3:
                    line(mat,
                         Point(iter1->box_x1,iter1->box_y1),
                         Point(iter1->box_x2,iter1->box_y2),
                         Scalar(iter1->box_color_b,iter1->box_color_g,
                                iter1->box_color_r),
                         iter1->box_line,
                         LINE_4);
                    break;
                default:
                    break;
                }
            }
        }

        frame = mat;
        writer << frame;
        capture >> mat;
        i++;
    }
    writer.release();
    capture.release();
    this->quit();
}
